#include <kutils.h>
#include <string.h>

s32int strtoint(s8int *str)
{
	s32int base = 1;
	s32int i = 0;
	for(i = 0; i < strlen(str) - 1; i++)
	{
		base *= 10;
	}
	s32int num = 0;
	s8int *tmp = str;
	while(*tmp != '\0')
	{
		num += (*tmp - 48) * base;
		base /= 10;
		tmp++;
	}
	return num;
}

void *strcpy(s8int *dest, const s8int *src)
{
	while(*src != '\0')//check that we don't overrun the source string
	{
		*dest = *src;
		dest++; src++;
	}
	*dest = '\0'; //null terminate
	return dest;
}

size_t strlen(const s8int *str)
{
    size_t retval;
    for(retval = 0; *str != '\0'; str++) retval++;
    return retval;
}

s32int strcmp(const s8int *str1, const s8int *str2)
{
	s32int difference = 0;
	while(1)
	{
		if(!( (*str1 == 0x00) == (*str2 == 0x00) ))
		{//one string has ended when the other has not
			difference = *str1 - *str2;
			return difference;
		}
		else
		{ //both strings are either the same length or are still going
			if((*str1 == 0x00) & (*str2 == 0x00))
			{//if they are the same length then they equal eachother
				return 0;
			}
			if(*str1 != *str2)
			{//we are not yet sure if they are the same length so check for differences in the letters
				difference = *str1 - *str2;
				return difference;
			}
		}
		str1++; str2++;		
	}
	return difference;
}

s8int* strcat (s8int* destination, const s8int* source)
{
	size_t dest_len = strlen(destination);
	size_t i;

	for (i = 0 ; source[i] != '\0' ; i++)
	{
		destination[dest_len + i] = source[i];
	}
	destination[dest_len + i] = '\0';

	return destination;
}
